//
//  WmcInviationAlertView.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/23.
//

import Foundation
import UIKit
import YPUIKit

/// 邀请加入弹窗
class WmcInviationAlertView: WmcBaseView {
    var joinBlock: ((_ isLogin: Bool) -> Void)?

    private var isLogin: Bool = false {
        didSet {
            updateUI()
        }
    }

    class func alert(note: String, teamName: String, isLogin: Bool, joinBlock: ((_ isLogin: Bool) -> Void)?) -> WmcInviationAlertView {
        let alert = WmcInviationAlertView()
        alert.noteLabel.text = note + "邀请你加入"
        alert.teamNameLabel.text = "【\(teamName)】"
        alert.joinBlock = joinBlock
        alert.isLogin = isLogin
        return alert
    }

    override func makeUI() {
        addSubview(noteLabel)
        addSubview(teamNameLabel)
        addSubview(instantJoinButton)
        addSubview(deleteButton)

        noteLabel.snp.makeConstraints { make in
            make.top.equalTo(55)
            make.centerX.equalToSuperview()
        }

        teamNameLabel.snp.makeConstraints { make in
            make.top.equalTo(noteLabel.snp.bottom).offset(8)
            make.centerX.equalToSuperview()
        }

        instantJoinButton.snp.makeConstraints { make in
            make.top.equalTo(teamNameLabel.snp.bottom).offset(30)
            make.left.equalTo(35)
            make.right.equalTo(-35)
            make.height.equalTo(44)
            make.bottom.equalTo(-33)
        }

        deleteButton.snp.makeConstraints { make in
            make.right.equalTo(-16)
            make.top.equalTo(16)
            make.height.width.equalTo(20)
        }
    }

    func updateUI() {
        if isLogin {
            instantJoinButton.title = "立即加入"
        } else {
            instantJoinButton.title = "登录并加入"
        }
    }

    override func bindViewAction() {
        instantJoinButton.rx.controlEvent(.touchUpInside).bind { [weak self] _ in
            guard let weakSelf = self, let block = weakSelf.joinBlock else { return }
            block(weakSelf.isLogin)
        }.disposed(by: rx.disposeBag)

        deleteButton.rx.controlEvent(.touchUpInside).bind { _ in
            YPApp.CurrentVC?.dismiss(animated: true)
        }.disposed(by: rx.disposeBag)
    }

    lazy var noteLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.wmc_grayH2_323233
        view.font = 18.yp.uiFont(weight: .semibold)
        return view
    }()

    lazy var teamNameLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.wmc_grayH2_323233
        view.font = 18.yp.uiFont(weight: .semibold)
        return view
    }()

    lazy var instantJoinButton: WmcNormalButton = {
        let view = WmcNormalButton()
        view.backgroundColor = UIColor.wmc_mainTheme
        view.font = 16.yp.uiFont
        view.textColor = UIColor.wmc_white_FFFFFF
        view.title = "立即加入"
        view.textAlignment = .center
        view.layer.cornerRadius = 4
        view.clipsToBounds = true
        return view
    }()

    lazy var deleteButton: WmcNormalButton = {
        let view = WmcNormalButton()
        view.imageView.image = "close".wmc_image
        view.imgSize = 20
        return view
    }()

    override func layoutSubviews() {
        super.layoutSubviews()
        let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: 8.yp.cgSize)
        let maskLayer = CAShapeLayer()
        maskLayer.path = maskPath.cgPath
        layer.mask = maskLayer
    }
}

extension WmcInviationAlertView: YPUIPresentTarget {
    func show() {
        let config = YPUIPresentController.Config()
        config.contentView = self
        config.transitionAnimated.usingSpringWithDamping = .init(appearDuration: 1, disappearDuration: 1)
        config.transitionAnimated.initialSpringVelocity = .init(appearDuration: 0, disappearDuration: 0)
        config.style = .sheet(.Create { make in
            make.left.right.bottom.equalToSuperview()
        })
        present(config: config)
    }
}
